package developer.算法.动态规划.零钱兑换;

import java.util.Arrays;

/**
 * @author zhangyongkang
 * @time 2025/4/8 16:58
 * @description 给你一个整数数组 coins ，表示不同面额的硬币；以及一个整数 amount ，表示总金额。
 * <p>
 * 计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额，返回 -1 。
 * <p>
 * 你可以认为每种硬币的数量是无限的。
 * <p>
 * 示例 1：
 * <p>
 * <p>
 * <p>
 * 输入：coins = [1, 2, 5], amount = 11
 * 输出：3
 * 解释：11 = 5 + 5 + 1
 * 示例 2：
 * <p>
 * 输入：coins = [2], amount = 3
 * 输出：-1
 * 示例 3：
 * <p>
 * 输入：coins = [1], amount = 0
 * 输出：0
 * <p>
 * <p>
 * 提示：
 * <p>
 * 1 <= coins.length <= 12
 * 1 <= coins[i] <= 231 - 1
 * 0 <= amount <= 104
 */
public class LingqianDuiHuan {

    public static void main(String[] args) {
        Solution3 solution = new Solution3();
        System.out.println(solution.coinChange(new int[]{1, 2, 5}, 11));
    }


    public class Solution5 {
        public int coinChange(int[] coins, int amount) {
            int[] dp = new int[amount + 1];

            for (int i = 1; i <= amount; i++) {
                dp[i] = Integer.MAX_VALUE;
                for (int j = 0; j < coins.length; j++) {
                    if (i > coins[j]) {
                        dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                    }
                }
            }
            return dp[amount] == Integer.MAX_VALUE ? 0: dp[amount];
        }

    }


    static class Solution4 {
        public int coinChange(int[] coins, int amount) {
            int[] dp = new int[amount + 1];


            for (int i = 1; i <= amount; i++) {
                for (int j = 0; j < coins.length; j++) {
                    int coin = coins[j];//硬币
                    dp[i] = Integer.MAX_VALUE;
                    if (coin <= i) {//影响的是比我小的
                        dp[i] = Math.min(dp[i], dp[i - coin] + 1);
                    }
                }
            }
            return dp[amount] == Integer.MAX_VALUE ? 0 : dp[amount];
        }
    }


    static class Solution3 {
        public int coinChange(int[] coins, int amount) {
            //要求的数量是最小的硬币
            int[] dp = new int[amount + 1];//每个最小的数量
            for (int i = 1; i <= amount; i++) {
                dp[i] = Integer.MAX_VALUE;
                for (int j = 0; j < coins.length; j++) {
                    if (coins[j] <= i) {
                        dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                    }
                }
            }
            return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
        }

    }

    static class Solution {
        public int coinChange(int[] coins, int amount) {
            if (amount == 0) return 0;
            int[] dp = new int[amount + 1];

            for (int i = 1; i <= amount; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 0; j < coins.length; j++) {
                    int coinVal = coins[j];//当前硬币的值
                    int dpLastIdx = i - coinVal;
                    if (dpLastIdx < 0) {
                        continue;
                    }
                    int lastVal = dp[dpLastIdx];
                    if (lastVal != 0) {
                        min = Math.min(min, dp[dpLastIdx] + 1);
                    } else {
                        if (coinVal == i) {
                            min = Math.min(min, 1);
                        }
                    }
                }
                if (min != Integer.MAX_VALUE) {
                    dp[i] = min;
                }
            }
            return dp[amount] == 0 ? -1 : dp[amount];
        }
    }

    /**
     * 作者：力扣官方题解
     * 链接：https://leetcode.cn/problems/coin-change/solutions/132979/322-ling-qian-dui-huan-by-leetcode-solution/
     * 来源：力扣（LeetCode）
     * 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
     */
    public class SolutionOfficial {
        public int coinChange(int[] coins, int amount) {
            int max = amount + 1;
            int[] dp = new int[amount + 1];
            Arrays.fill(dp, max);
            dp[0] = 0;
            for (int i = 1; i <= amount; i++) {
                for (int j = 0; j < coins.length; j++) {
                    if (coins[j] <= i) {
                        dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                    }
                }
            }
            return dp[amount] > amount ? -1 : dp[amount];
        }
    }

}
